package com.zjtaixue.o2o.util;

import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Slf4j
public class ImageUtil {

    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final Random random = new Random();
    private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();

    public static void main(String[] args) throws IOException {
        basePath = basePath.substring(1);
        Thumbnails.of(new File(basePath + "static/image/01_test.png")).size(200, 200)
                .watermark(Positions.BOTTOM_RIGHT,
                        ImageIO.read(new File(basePath + "static/image/02_test.png")), 0.5f).
                outputQuality(0.8f).
                toFile(basePath + "static/image/03_test.png");
        log.info("文件的输出路径={}", basePath + "static/image/03_test.png");
    }


    public static String generateThumbnail(InputStream file,String fileName, String targetAddress){
        String realFileName=getRandomFileName();
        String extension=getFileExtension(fileName);
        //相对路径
        String relativeAddress=targetAddress+realFileName+extension;
        File dest=new File(PathUtil.getImgBasePath()+relativeAddress);
        makeParentFolder(PathUtil.getImgBasePath()+relativeAddress);
        try{
            Thumbnails.of(file).size(200,200)
                    .watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/static/image/02_test.png")),0.25f)
                    .outputQuality(0.8f).toFile(dest);
        }catch (IOException e){
            log.error("文件io错误={}",e.getMessage());
            throw new RuntimeException(e);
        }
        return relativeAddress;
    }


    /**
     * 创建父文件路径
     * @param imagePath
     * @return
     */
    public static boolean makeParentFolder(String imagePath){
        File parentFile = new File(imagePath).getParentFile();
        if(parentFile != null){
            while (!parentFile.exists()){
                //批量生成全部的父文件路径
                parentFile.mkdirs();
            }
            return true;
        }
        return false;
    }

    /**
     * 获取文件输入流的扩展名
     * @param file
     * @return
     */
    private static String getFileExtension(File file){
        String originalFileName=file.getPath();
        return originalFileName.substring(originalFileName.lastIndexOf('.'));
    }

    private static String getFileExtension(String  fileName){
        return fileName.substring(fileName.lastIndexOf('.'));
    }

    /**
     * 生成随机文件名,当前的年月日小时分钟秒+五位随机数
     * @return
     */
    public static String getRandomFileName() {
        return simpleDateFormat.format(new Date())+(random.nextInt(89999)+10000);
    }

    /**
     * storePath是文件路径还是目录的路径
     * 如果storePath是文件路径则删除该文件
     * 如果storePath是目录路径则删除该目录下所有文件
     * @param storePath
     */
    public static void deleteFileOrPath(String storePath){
        File fileOrPath=new File(PathUtil.getImgBasePath()+storePath);
        if(fileOrPath.exists()){
            if(fileOrPath.isDirectory()){
                File[] files=fileOrPath.listFiles();
                for (int i=0;i<files.length;i++){
                    files[i].delete();
                }
            }
            fileOrPath.delete();
        }
    }
}
